import { message } from 'antd';
import React, { useEffect, useState } from 'react'

export default function useInsert(props:any) {
    const [isModalOpen, setIsModalOpen] = useState(false);
    useEffect(()=>{
        if(!isModalOpen){
            props.form.resetFields()
        }
    },[isModalOpen])
    const handleOk=async ()=>{
        let data=props.form.getFieldsValue(true);
        data=props.convertData?props.convertData(data):data;

        data.id ? props.updateData && (await props.updateData(data)) : props.createData && (await props.createData(data));

        props.success && props.success();

        message.success(data.id ? '更新成功':'创建成功');
        setIsModalOpen(false)
    }

    //回显数据
    const setDataInfo=async (id:any)=>{
        let data=await props.getDetail(id)
        // data=props.converDetailData && props.converDetailData(data)
        props.form.setFieldsValue(data);
        setIsModalOpen(true);
    }
  return {
      handleOk,
      setIsModalOpen,
      isModalOpen,
      setDataInfo
  }
}
